package v1

import (
	"DuckShop/internal/user/proto/users"
	"DuckShop/pkg/common"
	"DuckShop/pkg/logger"
	"DuckShop/pkg/middlewares"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
)

var UserAPILogger *zap.Logger

func newDuckUserClient() users.DuckUserServiceClient {
	dial, err := grpc.Dial(":8080", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatal(err.Error())
	}
	return users.NewDuckUserServiceClient(dial)
}
func Start() {
	// TODO 走配置文件
	UserAPILogger, err := logger.InitLogger("./api/user/logs/api.log", "./api/user/logs/err.log")
	if err != nil {
		panic(err)
	}
	/*
		初始化翻译器！
	*/
	err = common.InitLocaleTrans("zh")
	if err != nil {
		UserAPILogger.Fatal(err.Error())
	}

	r := gin.Default()
	r.Use(middlewares.APIRecover(UserAPILogger, true), middlewares.APILogger(UserAPILogger))
	InitUserRoutes(r, &UserAPIImpl{client: newDuckUserClient()})
	err = r.Run(":3000")
	if err != nil {
		return
	}
}
